package com.pentakill.mo.essay.essay.dao.impl;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.pentakill.mo.essay.essay.dao.dao.EssayDao;
import com.pentakill.mo.essay.essay.vo.EssayModel;
import com.pentakill.mo.essay.essay.vo.EssayQueryModel;
import com.pentakill.mo.util.base.BaseImpl;
import com.pentakill.mo.util.base.BaseQueryModel;

public class EssayImpl extends BaseImpl<EssayModel> implements EssayDao{

	protected void doQbc(DetachedCriteria criteria, BaseQueryModel qm) {
		//未添加查询条件只添加了通过审核的文章
		EssayQueryModel eqm = (EssayQueryModel) qm;
		criteria.add(Restrictions.eq("status", 3));
	}
	
	/**
	 * 查询通过审核的qbc
	 */
	private void doQbcByPass(DetachedCriteria criteria, BaseQueryModel qm) {
		
		EssayQueryModel eqm = (EssayQueryModel) qm;
		criteria.add(Restrictions.eq("status", 3));
		criteria.add(Restrictions.eq("uuid",eqm.getUuid()));
		
	}
	
	
	public List<EssayModel> getShowPassLlis(BaseQueryModel eqm, Integer pageNum, Integer pageCount) {
		DetachedCriteria criteria = DetachedCriteria.forClass(EssayModel.class);
		doQbcByPass(criteria,eqm);
		List<EssayModel> essList = this.getHibernateTemplate().findByCriteria(criteria);
		return essList;
	}

	public EssayModel getByPass(BaseQueryModel eqm) {
		DetachedCriteria criteria = DetachedCriteria.forClass(EssayModel.class);
		doQbcByPass(criteria,eqm);
		List<EssayModel> essList = this.getHibernateTemplate().findByCriteria(criteria);
		return essList.size()>0 ?essList.get(0): null ;
	}

	public int getPassCount(BaseQueryModel eqm) {
		DetachedCriteria criteria = DetachedCriteria.forClass(EssayModel.class);
		criteria.setProjection(Projections.rowCount());
		doQbcByPass(criteria, eqm);
		List<Long> longs = this.getHibernateTemplate().findByCriteria(criteria);
		return longs.get(0).intValue();
	}
	
}
